import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import cookie from "js-cookie";
import customerApi from "@/api/customerlogin";
const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: []
  },
  customer:{},

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    }
  },

  actions: {
    // 登录
    Login({ commit }, loginForm) {
      const phone = loginForm.phone.trim()
      // console.log(phone)
      // console.log(loginForm.password)
      console.log(loginForm)
      return new Promise((resolve, reject) => {
        login(loginForm).then(response => {
          const data = response.data
          console.log(data)
          // cookie.set("noqueue_token",data.token,{domain:"localhost"})
          setToken(data.token)  //这里已经将token存入名为Admin-Token的cookie里面
          commit('SET_TOKEN', data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        customerApi.getUserInfo().then(response => {
          // console.log(response.data)
          this.customer = response.data
          // console.log(state.token)//这里的token字符串没有存入cookie中去，会导致用户信息取出来为空
          if (state.token && state.token.length > 0) { // 判断token字符串是否为空，如果为空，说明登录失败
            //将用户信息存入名为noqueue_customerInfo的cookie中
            cookie.set("noqueue-customerInfo", JSON.stringify(this.customer), { domain: "localhost" });
          } else {
            reject('登录失败！')
          }
          // commit('SET_NAME', response.data.username)
          // commit('SET_AVATAR', data.avatar)

          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })

      // return new Promise((resolve, reject) => {
      //   customer.getUserInfo.then(response => {
      //     this.customer = response.data.customerInfo
      //     console.log(response.data)
      //     const data = response.data
      //     console.log(state.token)//这里的token字符串没有存入cookie中去，会导致用户信息取出来为空
      //     if (state.token && state.token.length > 0) { // 判断token字符串是否为空，如果为空，说明登录失败
      //       // commit('SET_ROLES', data.token)
      //     } else {
      //       reject('登录失败！')
      //     }
      //     // commit('SET_NAME', data.name)
      //     // commit('SET_AVATAR', data.avatar)

      //     resolve(response)
      //   }).catch(error => {
      //     reject(error)
      //   })
      // })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        cookie.set('Admin-Token','',{domain:"localhost"})
        cookie.set('noqueue-customerInfo','',{domain:"localhost"})
        window.location.href="/"
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
